package com.mon.test;

import java.io.IOException;

import javassist.CannotCompileException;
import javassist.ClassPool;
import javassist.CtClass;
import javassist.CtField;
import javassist.CtMethod;
import javassist.CtNewMethod;
import javassist.Modifier;
import javassist.NotFoundException;
import java.util.HashMap;

public class JassistMain {
	
	
	public static void main(String args[]) throws NotFoundException, CannotCompileException, IOException{
		ClassPool cp = ClassPool.getDefault();
        CtClass cc = cp.get("com.mon.test.Address");
        CtMethod[] ms=cc.getDeclaredMethods();
        
        
//        CtClass countMap = ClassPool.getDefault().get("com.mon.test.MonitorCountResponse");        
//        CtField fl = new CtField(countMap, "countResponse", cc);        
//        fl.setModifiers(Modifier.STATIC);       
//        cc.addField(fl,CtField.Initializer.byExpr("new com.mon.test.MonitorCountResponse();"));
     
        System.out.println(cc.getName());
        System.out.println(cc.getName().replace('.', '_'));
       // String[] varname=new String[ms.length];
        String varname = "";
        int track=0;
        StringBuilder mapCode=new StringBuilder();
        for(CtMethod cm:ms){
        	varname=(cc.getName()+"."+cm.getName()+"."+"Counter").replace('.', '_');
        	
        	CtField f = new CtField(CtClass.longType,varname, cc);        	
        	f.setModifiers(Modifier.STATIC); 	
        	
        	cc.addField(f,CtField.Initializer.byExpr("0L"));
        	//cc.rebuildClassFile();  
        	//cm.insertBefore("ht++;");
        	cm.insertBefore(varname+"++;"); 
        	
        	//create code
        	/*mapCode.append("countMap.put(\"");
        	mapCode.append(varname);
        	mapCode.append("\",");
        	mapCode.append(varname);
        	mapCode.append(");");
        	System.out.println(mapCode.toString());*/
        	//mapcode="countMap.put("+varname+");"
        	
        	
        	
        	mapCode.append("\"");
        	mapCode.append(varname);     
        	mapCode.append("$");
        	mapCode.append("\"");        	
        	mapCode.append("+");
        	mapCode.append(varname);
        	
        	
        	if(cm!=ms[ms.length-1]){
        		mapCode.append("+");
        		mapCode.append("\":\"");
        		mapCode.append("+");
			}
        	System.out.println(mapCode.toString());
        	
        }
     
        
        /*
        CtMethod method = CtNewMethod.make("public static void getAllCount() { System.out.println(com_mon_test_Address_getLname_Counter); }", cc);
       
        cc.addMethod(method);*/
        

     //   CtMethod method = CtNewMethod.make("public static java.util.HashMap getAllCount() { "+mapCode.toString()+"return countMap; }", cc);
       
        String temp=mapCode.toString();
       CtMethod method = CtNewMethod.make("public static String getAllCount() {  return "+temp +"; }", cc);
        cc.addMethod(method);
        


        
      //  ms[0].insertBefore("ht"+"++;");
//        CtMethod m = cc.getDeclaredMethod("getFname");
//        m.addLocalVariable("elapsedTime", CtClass.longType);
//        m.insertBefore("elapsedTime = System.currentTimeMillis();");
//        m.insertAfter("{elapsedTime = System.currentTimeMillis() - elapsedTime;"
//                + "System.out.println(\"Method Executed in ms: \" + elapsedTime);}");
        
        cc.writeFile("d://");
        
	}

}
